/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package filetransserv;

import DAO.UserDAO;
import java.io.BufferedInputStream;
import javax.xml.stream.XMLStreamException;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFormattedTextField;
import javax.swing.JList;
import javax.swing.ListModel;
import Model.User;
import xmlwriter.CommandBuilder;

/**
 *
 * @author carlosscipione
 */
public class ServerManager {

    private boolean flag = false;
    private int modelSize;
    private String[] StatusString;
    private ListModel model;
    public static int ThreadCount;

    private List<User> users;

    public ServerManager() {
        
    }

    public void authenticate(Socket socket, String nickname, String pass) throws XMLStreamException {
        try {

            String message = null;

            //Checkeo que el usuario sea correcto en los archivos XML
            UserDAO user = UserDAO.getInstance();

            if (user.Exist(nickname, pass)) {

                CommandBuilder response = new CommandBuilder(socket);
                response.BuildResponseMessage("Yes").flush();
            }
            else {

                CommandBuilder response = new CommandBuilder(socket);
                response.BuildResponseMessage("No").flush();
            }

            socket.close();

        } catch (IOException e) {
            //nothing
        }

    }
    
    public void register(Socket socket, String nickname, String pass) throws XMLStreamException {
        try {

            String message = null;

            //Checkeo que el usuario sea correcto en los archivos XML
            UserDAO user = UserDAO.getInstance();

            if (user.createUser(nickname, pass)) {

                CommandBuilder response = new CommandBuilder(socket);
                response.BuildResponseMessage("Yes").flush();
            }
            else {

                CommandBuilder response = new CommandBuilder(socket);
                response.BuildResponseMessage("No").flush();
            }

            socket.close();

        } catch (IOException e) {
            //nothing
        }

    }


    public void getLoad(Socket socket) throws XMLStreamException {
        try {

            String message = Integer.toString(this.ThreadCount);

            CommandBuilder response = new CommandBuilder(socket);
            response.BuildResponseMessage(message).flush();

            socket.close();

        } catch (IOException e) {
            //nothing
        }

    }


    public void getFiles(Socket socket) {
        try {

            File dir = new File(System.getProperty("user.dir") + "/files");
            File[] isFolder = dir.listFiles();
            int count = 0;
            for (int i = 0; i < isFolder.length; i++) {
                if (isFolder[i].isDirectory() || isFolder[i].isHidden()) {
                    count++;
                }
            }

            String[] files = new String[isFolder.length - count];
            count = 0;
            for (int i = 0; i < isFolder.length; i++) {
                if (!isFolder[i].isDirectory() && !isFolder[i].isHidden()) {
                    files[count] = isFolder[i].getName();
                    count++;
                }
            }

            

            String message = null;
            ObjectOutputStream oos = new ObjectOutputStream(
                    socket.getOutputStream());
            message += files.length;
            message += "@end|begin@";
            if (files != null) {
                for (int i = 0; i < files.length; i++) {
                    message += files[i];
                    message += "@end|begin@";
                }
            } else {
                message = "Empty";
            }
            oos.writeObject(message);



            oos.close();

            socket.close();

        } catch (IOException e) {
            //nothing
        }

    }


    public void Download(Socket socket, String nickname, String file, int block) {
        try {
            OutputStream os = null;

            ObjectOutputStream oos = new ObjectOutputStream(
                    socket.getOutputStream());
            File myFile = new File(System.getProperty("user.dir") + "/files/" + file);

            oos.writeObject((Long) myFile.length());

            BufferedInputStream bis;
            BufferedOutputStream bos;
            int in;
            byte[] byteArray;

            try {


        //        Input = new DataInputStream (client.getInputStream() );

                bis = new BufferedInputStream(new FileInputStream(System.getProperty("user.dir") + "/files/"
                        + file));
                bos = new BufferedOutputStream(socket.getOutputStream());
                byteArray = new byte[8192];
                int i = 0;

                if (block > 0) {

                    while (i < block + 1) {
                        in = bis.read(byteArray);
                        i++;
                    }
                }

                while ((in = bis.read(byteArray)) != -1) {
                    bos.write(byteArray, 0, in);
                }

                bis.close();
                bos.close();

            } catch (Exception e) {
                System.err.println(e);
            }




            os.close();
            // oos.close();




        } catch (IOException e) {
            //nothing
        } catch (OutOfMemoryError e) {
            //nothing
        } catch (Exception e) {
            //nothing
        }


    }
    
}
